<template>
	<view v-show="isNewUser">
		<view class="zhezhao">

		</view>
		<view class="login-p">
			<view class="login-content">
				<view class="login-title">
					温馨提示
				</view>
				<view class="login-message">
					您需注册会员后才能使用此功能，请在店长指导下进行注册~
				</view>
			</view>
			<view class="login-button" @click="to_register">
				注册
			</view>
		</view>
	</view>
</template>

<script >
	export default {
		props:{
			title: {
				type: String,
				default: ''
			},
            sourcePage: {
                type: String,
				default: ''
            },
			source: {
				type: Number,
				default: null
			},
			invitorCode: {
				type: String,
				default: ''
			},
		},
		data() {
			return {
				code: "",
				isNewUser:false
			}
		},
		created() {
			this.get_wx_code()
			this.login();
			if(!this.title){
				this.title=""
			}
		},
		methods: {
			//#ifdef H5
			login() {
				// 获取微信code
				console.log("html 登录")
				var appid="wx882ef50fd0e0cbc3";
				var secret="5c0f5912072d9d65f31149ef5adfd6fa"
				this.getCode()
			},
			 getUrlCode() {
				// 截取url中的code方法
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf('?') != -1) {
					var str = url.substr(1);
					var strs = str.split('&');
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
					}
				}
				console.log(theRequest);
				return theRequest;
			},
			
			 getCode() {
				// 非静默授权，第一次有弹框
				this.code = '';
				var local = window.location.href; // 获取页面url
				var appid = 'wx882ef50fd0e0cbc3';
				let id = 1
				var url = encodeURIComponent(local);
				var url = "https://mes.renkangshangcheng.cn/";
				this.code = this.getUrlCode(); // 截取codeurlEncode
				if (JSON.stringify(this.code) == '{}') {
					// 如果没有code，则去请求
					window.location.href =
						'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
						appid +
						'&redirect_uri=' +
						url +
						'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
				} else {
					let that = this
				console.log(this.code);
				}
			},
			//#endif
			//#ifdef MP-WEIXIN
			login() {
				// 获取微信code
				var _this=this;
				uni.login({
				  provider: 'weixin',
				  success: function (loginRes) {
			          _this.check_login(loginRes.code);
				}})
			},
			//#endif
		
			// 检测登录
			check_login(code) {
			    var _this = this;
				var data = {
					code:code,
					requestCode:''
				};
				//#ifdef H5
				data.isH5=1;
				//#endif
				this.$NoteApi.doRequest("get", "login", data).then(res => {
					if(res.data.code == 0){
						// 1 新用户 去注册 0 老用户
						if(res.data.data.isNew == 1) {
							this.isNewUser=true;
						}else {
							uni.setStorageSync("token", res.data.data.token)
							uni.setStorageSync("userInfo", res.data.data);
							this.$emit("plogin");
						}
					}else {
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					}
				})
			},
			no_login() {
				uni.setStorageSync("token",false)
				this.$emit("x");
			},
			get_wx_code() {
				var _this = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						_this.wx_code = loginRes.code;
					}
				})
			},
			to_register(){
				uni.navigateTo({
					url:`/pages/newAnswer/register?type=index&source=${this.source}&sourcePage=${this.sourcePage}&invitorCode=${this.invitorCode}`
				});
			}
		}
	}
</script>

<style scoped>
	.zhezhao {
		width: 100%;
		height: 100%;
		background-color: #000000;
		opacity: 0.5;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
	}
	.login-p {
		width: 610upx;
		height: 604upx;
		background-image: url(https://wechat-pictures-1301970825.file.myqcloud.com/wxapp/login/tankuang_bg_tuangou@2x.png);
		background-size: 100% 100%;
		z-index: 1000;
		position: fixed;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
	}
	.login-content{
		position: absolute;
		top: 60upx;
		left: 50%;
		transform: translate(-50%,0%);
		width: 100%;
		box-sizing: border-box;
		padding: 0 73upx;
	}
	.login-title{
		font-size: 46upx;
		color: #fff;
		text-align: center;
		font-weight: 500;
	}
	.login-message{
		font-size: 46upx;
		color: #fff;
		font-weight: 400;
		line-height: 65upx;
		margin-top: 20upx;
	}
	.login-button{
		width: 478upx;
		height: 96upx;
		position: absolute;
		bottom: 33upx;
		left: 50%;
		transform: translate(-50%,0%);
		background-image: url(https://wechat-pictures-1301970825.file.myqcloud.com/wxapp/login/tankuang_tab_sure@2x.png);
		text-align: center;
		line-height: 96upx;
		background-size: 100% 100%;
		font-size: 46upx;
		color: #fff;
		font-weight: 500;
	}
	.quxiao{
		position: absolute;
		width: 76upx;
		height: 76upx;
		top: -144upx;
		right: 0upx;
	}

	
</style>
